package com.school.information.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.information.entity.ModuleInfoEntity;
import com.school.information.entity.ModuleLabelEntity;
import com.school.information.entity.SysUserEntity;
import com.school.information.enums.result.ModuleResultEnum;
import com.school.information.qo.ModuleLabelQO;
import com.school.information.service.ModuleInfoService;
import com.school.information.service.ModuleLabelService;
import com.school.information.service.SysUserService;
import com.school.information.utils.NumberUtil;
import com.school.information.utils.ResultUtil;
import com.school.information.vo.ModuleLabelVO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * (ModuleLabel)表控制层
 *
 * @author ycc
 * @since 2023-07-10 10:32:06
 */
@Slf4j
@RestController
@RequestMapping("module/label")
public class ModuleLabelController {
    /**
     * 服务对象
     */
    @Resource
    private ModuleLabelService moduleLabelService;
    @Resource
    private SysUserService userService;
    @Resource
    private ModuleInfoService moduleInfoService;

    /**
     * 新增模块标签信息
     *
     * @param moduleLabel
     * @return
     */
    @PostMapping("/save")
    public ResultUtil save(@RequestBody @Valid ModuleLabelEntity moduleLabel) {
        log.info("##moduleLabel={}", moduleLabel);
        if (Objects.isNull(moduleLabel)) {
            log.error("##参数错误，模块标签信息数据为空");
            return ResultUtil.ERROR_ARG;
        }
        if (moduleLabelService.saveInfo(moduleLabel)) {
            return ResultUtil.SUCCESS_NO_DATA;
        }
        log.error("##模块标签信息新增失败");
        return ResultUtil.error(ModuleResultEnum.ADD_MODULE_LABEL_FAIL);
    }

    /**
     * 更新模块标签信息
     *
     * @param moduleLabel
     * @return
     */
    @PutMapping("/update")
    public ResultUtil update(@RequestBody @Valid ModuleLabelEntity moduleLabel) {
        log.info("##moduleLabel={}", moduleLabel);
        if (Objects.isNull(moduleLabel) || NumberUtil.isNullOrLessEqual0(moduleLabel.getId())) {
            log.error("##参数错误，模块标签信息数据为空");
            return ResultUtil.ERROR_ARG;
        }
        if (moduleLabelService.updateInfo(moduleLabel)) {
            return ResultUtil.SUCCESS_NO_DATA;
        }
        log.error("##模块信息id={}更新失败", moduleLabel.getId());
        return ResultUtil.error(ModuleResultEnum.UPDATE_MODULE_LABEL_FAIL);
    }

    /**
     * 分页查询所有数据
     *
     * @param moduleLabel 查询实体
     * @return 所有数据
     */
    @PostMapping("/pageList")
    public ResultUtil pageList(@RequestBody ModuleLabelQO moduleLabel) {
        IPage<ModuleLabelEntity> page = moduleLabelService.pageList(moduleLabel);
        if (CollectionUtil.isEmpty(page.getRecords())) {
            return ResultUtil.success(page);
        }
        // 获取用户信息map
        Set<Long> userIds = page.getRecords().stream().map(ModuleLabelEntity::getUpdateUser).collect(Collectors.toSet());
        Map<Long, SysUserEntity> userMap = userService.getUserMap(new ArrayList<>(userIds));

        // 获取所有模块信息map
        Set<Long> moduleInfoIds = page.getRecords().stream().map(ModuleLabelEntity::getModuleInfoId).collect(Collectors.toSet());
        Map<Long, ModuleInfoEntity> moduleInfoMap = moduleInfoService.getModuleInfoMap(new ArrayList<>(moduleInfoIds));

        List<ModuleLabelVO> list = new LinkedList<>();
        page.getRecords().forEach(item -> {
            ModuleLabelVO moduleLabelVO = new ModuleLabelVO();
            BeanUtils.copyProperties(item, moduleLabelVO);
            moduleLabelVO.setUpdateBy(userMap.get(item.getUpdateUser()).getRealName());
            moduleLabelVO.setModuleInfoName(moduleInfoMap.get(item.getModuleInfoId()).getName());
            list.add(moduleLabelVO);
        });
        IPage<ModuleLabelVO> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        resultPage.setRecords(list);
        return ResultUtil.success(resultPage);
    }

    /**
     * 根据id，查询模块信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultUtil getById(@PathVariable("id") Long id) {
        if (NumberUtil.isNullOrLessEqual0(id)) {
            log.error("##参数错误，用户id不存在");
            return ResultUtil.ERROR_ARG;
        }
        return ResultUtil.success(moduleLabelService.getById(id));
    }

    /**
     * 删除模块信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public ResultUtil delete(@PathVariable("ids") Long[] ids) {
        if (ArrayUtils.isEmpty(ids)) {
            return ResultUtil.ERROR_ARG;
        }
        if (moduleLabelService.removeByIds(Arrays.asList(ids))) {
            return ResultUtil.SUCCESS_NO_DATA;
        }
        log.error("##模块标签id={}删除失败", ids);
        return ResultUtil.error(ModuleResultEnum.DEL_MODULE_LABEL_FAIL);
    }
}

